package cn.iocoder.ny.module.mall.service.impl;

import cn.iocoder.ny.module.mall.controller.admin.sms.vo.MallSmsVo;
import cn.iocoder.ny.module.mall.domain.SmsReport;
import cn.iocoder.ny.module.mall.domain.SmsReportRequest;
import cn.iocoder.ny.module.mall.enums.SmsEnum;
import cn.iocoder.ny.module.mall.service.IMallSmsService;
import cn.iocoder.ny.module.mall.utils.AliSmsUtils;
import cn.iocoder.ny.module.mall.utils.SmsErrorCodeUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.core.RedisKeyValueTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.ny.module.mall.enums.SmsEnum.SAAS;

/**
 * @author luchunhong
 * @date 2024/10/22/ 10:53
 * @description:
 */
@Service
public class IMallSmsServiceImpl implements IMallSmsService {

    @Value("${alisms.accessKeyId}")
    private String accessKeyId;
    @Value("${alisms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${alisms.signName}")
    private String signName;
    @Value("${alisms.templateCode}")
    private String templateCode;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public String sendMessageCode(MallSmsVo mallSmsVo) throws Exception {

        String num = AliSmsUtils.sendSms(mallSmsVo.getMobile(), 6, signName,templateCode, accessKeyId, accessKeySecret);
        if (null==num) {
            throw new Exception("验证码获取失败");
        }
        // 配置redis缓存
        redisTemplate.opsForValue().set(SAAS+mallSmsVo.getMobile(),num,5, TimeUnit.MINUTES);
        return num;
    }

    @Override
    public Boolean sendMessageCodeByTyy(SmsReport smsReport) {
        String reportCode = smsReport.getReportCode();
        if (reportCode.equals("OK")||reportCode.equals("DELIVRD")) {
            redisTemplate.opsForValue().set(SAAS + smsReport.getPhone(), smsReport.getSessionId(), 5, TimeUnit.MINUTES);
            return true;
        }
//         if (reportCode.equals("30022")) {
//             redisTemplate.opsForValue().set("SAAS"+smsReport.getPhone(), "BALANCE_INSUFFICIENT", 5, TimeUnit.MINUTES);
//            return false;
//         }
        String errorMessage = SmsErrorCodeUtil.getErrorMessage(Integer.parseInt(reportCode));
        redisTemplate.opsForValue().set(SAAS+smsReport.getPhone(), errorMessage, 5, TimeUnit.MINUTES);
        return null;
    }
    @Override
    public Boolean getMessageCodeByTyy(String mobile) throws Exception {
        String value = (String) redisTemplate.opsForValue().get(SAAS + mobile);
        if (!ObjectUtils.isEmpty(value)){
            if (value.matches("^\\d{6}$")) {
                return true;
            }
//            if (value.startsWith("BALANCE_INSUFFICIENT")) {
//                throw new Exception("短信余额不足");
//            }
                throw new Exception(value);

        }
        return null;
    }
}
